package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkGroupCheckitemMapper;
    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    /**
     * 带条件（编码/名称/助记码）分页查询检查组
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupMapper.getByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        // 根据检查组id查询检查组
        CheckGroup checkGroup = checkgroupMapper.findById(id);

        // 根据检查组id查询关联的检查项id
        List<Integer> checkItemIds = checkGroupCheckitemMapper.findCheckItemIdsByCheckGroupId(id);
        List<CheckItem> checkItems = new ArrayList<>();
        for (Integer checkItemId : checkItemIds) {
            // 根据检查项id查询检查项
            CheckItem checkItem = checkitemMapper.findById(checkItemId);
            checkItems.add(checkItem);
        }
        checkGroup.setCheckItems(checkItems);
        return checkGroup;
    }

    /**
     * 新增检查组
     *
     * @param checkitemIds
     * @param checkGroup
     */
    @Transactional
    @Override
    public void add(List<Integer> checkitemIds, CheckGroup checkGroup) {
        // 插入检查组
        checkgroupMapper.insert(checkGroup);

        // 获取主键返回的检查组id
        Integer checkGroupId = checkGroup.getId();
        // 根据获取的检查组id和检查项id插入检查组关联的检查项
        if (checkitemIds != null && checkitemIds.size() > 0) {
            checkGroupCheckitemMapper.add(checkitemIds, checkGroupId);
        }
    }

    /**
     * 根据id删除检查组
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        // 先根据检查组id查询该检查组是否被套餐关联
        Long count = setmealCheckgroupMapper.findCountByCheckgroupId(id);
        if (count > 0){
            // 有关联
            throw  new RuntimeException("该检查组被套餐关联，不能删除");
        }else{
            // 没有关联
            // 删除检查组关联的检查项
            checkGroupCheckitemMapper.deleteByCheckGroupId(id);
            // 删除检查组
            checkgroupMapper.deleteById(id);
        }
    }


    /**
     * 编辑检查组
     *
     * @param checkGroup
     */
    @Override
    @Transactional
    public void edit(List<Integer> checkitemIds, CheckGroup checkGroup) {
        // 编辑检查组
        checkgroupMapper.edit(checkGroup);
        // 删除关联的检查项信息
        checkGroupCheckitemMapper.deleteByCheckGroupId(checkGroup.getId());
        // 重新插入关联的检查项信息
        checkGroupCheckitemMapper.add(checkitemIds, checkGroup.getId());
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkgroupMapper.findAll();
        return checkGroups;
    }

    /**
     * 根据套餐id查询关联的检查组id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> checkgroupIds = setmealCheckgroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        return checkgroupIds;
    }
}
